<template>
  <transition name="componentTab">
    <div class="store" v-if="storeInfo" @scroll="scrollEvent">
      <header class="header" :class="{ 'header-scroll': scrollTop >= 100 }">
        <div class="left" @click.stop="$router.go(-1)">
          <svg
            t="1675766060581"
            class="icon"
            viewBox="0 0 1024 1024"
            version="1.1"
            xmlns="http://www.w3.org/2000/svg"
            p-id="3763"
            width="16"
            height="16"
          >
            <path
              d="M393.390114 512.023536l347.948667-336.348468c20.50808-19.85828 20.50808-51.997258 0-71.792093-20.507056-19.826558-53.778834-19.826558-74.28589 0L281.990954 476.135164c-20.476357 19.826558-20.476357 51.981908 0 71.746044l385.061936 372.236839c10.285251 9.91379 23.728424 14.869662 37.173644 14.869662 13.446243 0 26.889417-4.956895 37.112246-14.901385 20.50808-19.826558 20.50808-51.919487 0-71.746044L393.390114 512.023536"
              p-id="3764"
              fill="#ffffff"
            ></path>
          </svg>
        </div>
        <div class="right">
          <div class="shop" @click.stop="$router.push('/myshop')">
            <van-badge :content="myShop.length" max="99">
              <van-icon class="icon" name="shopping-cart-o" />
            </van-badge>
          </div>
        </div>
      </header>
      <div class="swipe">
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
          <van-swipe-item v-for="(item, index) in storeInfo.p.is" :key="index">
            <img :src="item" alt="" />
          </van-swipe-item>
        </van-swipe>
      </div>
      <div class="store-content">
        <div class="store-title">{{ storeInfo.p.t }}</div>
        <div class="des">
          <div
            v-for="(desText, index) in storeInfo.p.des.split(/\s/g)"
            :key="index"
          >
            {{ desText }}
          </div>
        </div>
        <div class="store-price">
          <span class="p">&yen;{{ storeInfo.p.p }}</span>
          <span class="op">&yen;{{ storeInfo.p.op }}</span>
          <span class="tag">{{ storeInfo.p.nsp.replace("运费 ", "") }}</span>
        </div>
        <div class="store-vip">
          <img src="@/../public/images/store/1676527585407.png" alt="" />
          <div class="vip-text">{{ storeInfo.p.rebate }}</div>
        </div>
        <div class="store-sell">
          <div>{{ storeInfo.p.si }}</div>
          <div>{{ storeInfo.p.add }}</div>
        </div>
      </div>
      <div class="promotion">
        <div class="left">促销</div>
        <div class="right">
          <div
            class="promotion-item"
            v-for="(item, index) in storeInfo.p.atl"
            :key="index"
          >
            <div class="promotion-item-tag">{{ item.t }}</div>
            <div class="promotion-item-text">{{ item.c }}</div>
          </div>
        </div>
      </div>
      <van-cell is-link class="my-cell" @click.stop="changeShowShop">
        <template #title>
          <div class="my-select">
            <div class="custom-title">尺寸</div>
            <div class="sel-text">已选 原味2瓶</div>
          </div>
        </template>
      </van-cell>
      <van-cell is-link class="my-cell">
        <template #title>
          <div class="my-select">
            <div class="custom-title">配送至</div>
            <div class="sel-text">已选 原味2瓶</div>
          </div>
        </template>
      </van-cell>
      <div class="comments" v-if="storeInfo?.p?.sos">
        <div class="comments-header">
          <div class="left">
            <span class="title">评价晒单</span
            ><span>({{ storeInfo.p.sos.length || 0 }})</span>
          </div>
          <div class="right" @click.stop="showAllComment = true">
            更多<van-icon name="arrow" />
          </div>
        </div>
        <div class="comment-list">
          <StoreCommentItem
            v-for="(item, index) in storeInfo.p.sos.slice(0, 3)"
            :key="index"
            :comment="item"
          />
        </div>
        <transition name="jumpTop">
          <div v-if="showAllComment" class="all-comment-list">
            <StoreAllComment
              :commentList="storeInfo.p.sos"
              @changeShow="showAllComment = false"
            />
          </div>
        </transition>
      </div>
      <div class="shoper" v-if="storeInfo.p.store">
        <div class="shoper-title">店铺信息</div>
        <div class="shoper-info">
          <div class="left">
            <img :src="storeInfo.p.store.l" alt="" />
          </div>
          <div class="midle">
            <div>{{ storeInfo.p.store.n }}</div>
            <div>{{ storeInfo.p.store.sfi }}</div>
          </div>
          <div class="right">进店逛逛</div>
        </div>
        <div class="shoper-ss">
          <div
            class="ss-item"
            v-for="(item, index) in storeInfo.p.store.ss"
            :key="index"
          >
            <span>{{ item.t }}</span>
            <div>{{ item.s }}</div>
          </div>
        </div>
      </div>
      <footer class="footer">
        <div class="chat-icon">
          <van-icon class="icon" name="chat-o" />
          <div>联系卖家</div>
        </div>
        <div class="chat-icon">
          <van-icon class="icon" name="shop-o" />
          <div>店铺</div>
        </div>
        <div class="btn" @click.stop="changeShowShop">加入购物车</div>
        <div class="btn" @click.stop="changeShowShop">立即购买</div>
      </footer>
      <van-popup
        closeable
        round
        v-model="showShop"
        position="bottom"
        :style="{ height: '4.3rem' }"
      >
        <div class="popup-content">
          <div class="goods-info">
            <div
              class="img-box"
              :style="{ backgroundImage: `url(${storeInfo.p.ti})` }"
            ></div>
            <div class="right">
              <div class="title">{{ storeInfo.p.t }}</div>
              <div class="price">
                <span class="p">&yen;{{ storeInfo.p.p }}</span
                ><span class="op">&yen;{{ storeInfo.p.op }}</span>
              </div>
            </div>
          </div>
          <div class="specification">
            <div class="specification-title">规格</div>
            <div class="specification-items" v-if="storeInfo.p.sps?.length">
              <div
                :class="{ active: goodTag.id == item.id }"
                v-for="(item, index) in storeInfo.p.sps"
                :key="index"
                @click.stop="goodTag = item"
              >
                {{ item.sd }}
              </div>
            </div>
            <div class="specification-items" v-else><div>暂无规格</div></div>
          </div>
          <div class="count">
            <div class="count-title">购买数量</div>
            <van-stepper v-model="goodValue" min="1" max="99" />
          </div>
          <van-button
            class="btn"
            round
            color="var(--bgc-yellow)"
            type="primary"
            size="large"
            @click.stop="addMyShop"
            >确定</van-button
          >
        </div>
      </van-popup>
    </div>
  </transition>
</template>

<script>
import qs from "qs";
import StoreCommentItem from "@/components/store/StoreCommentItem.vue";
import StoreAllComment from "@/components/store/StoreAllComment.vue";
export default {
  components: {
    StoreCommentItem,
    StoreAllComment,
  },
  data: () => {
    return {
      showShop: false,
      showAllComment: false,
      scrollTop: 0,
      storeInfo: null,
      goodValue: 1,
      goodTag: null,
      myShop: [],
    };
  },
  methods: {
    //添加购物车
    addMyShop() {
      if (!this.storeInfo.p.sps?.length) {
        this.$toast("暂无商品");
        return;
      }
      this.myShop = JSON.parse(localStorage.getItem("myShop")) || [];
      this.myShop = [
        {
          id: this.storeInfo.p.id,
          store: this.storeInfo.p.store,
          img: this.storeInfo.p.ti,
          title: this.storeInfo.p.t,
          p: this.storeInfo.p.p,
          op: this.storeInfo.p.op,
          atl: this.storeInfo.p.atl,
          goodTag: this.goodTag,
          goodValue: this.goodValue,
          isChecked:false
        },
        ...this.myShop.filter((item) => item.goodTag.id != this.goodTag.id),
      ];
      localStorage.setItem("myShop", JSON.stringify(this.myShop));
      this.$toast.success("添加成功");
    },
    changeShowShop() {
      this.showShop = true;
    },
    scrollEvent(e) {
      this.scrollTop = e.target.scrollTop;
    },
    getStoreInfo() {
      this.axios({
        method: "post",
        url: `${this.api.baseUrlV2}/products/detail`,
        data: qs.stringify({
          id: this.$route.params.storeid,
        }),
        headers: {
          version: this.api.version,
          "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
        },
      }).then((res) => {
        this.storeInfo = res.data.result;
        this.goodTag = this.storeInfo.p.sps[0];
      });
    },
  },
  activated() {
    this.myShop = JSON.parse(localStorage.getItem("myShop")) || [];
    this.getStoreInfo();
  },
};
</script>

<style lang="scss" scoped>
.store {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: var(--bgc-gray);
  z-index: 100;
  padding-bottom: 1rem;
  box-sizing: border-box;
  overflow-y: scroll;
  &::-webkit-scrollbar {
    display: none;
  }
  .header {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 100;
    display: flex;
    height: 0.5rem;
    width: 100%;
    align-items: center;
    box-sizing: border-box;
    padding: 0.1rem;
    transition: 0.3s;
    color: var(--text-white);
    background-image: linear-gradient(
      to bottom,
      rgba(0, 0, 0, 0.6),
      rgba(0, 0, 0, 0)
    );
    .left {
      width: 0.2rem;
      min-width: 0.2rem;
      height: 0.38rem;
    }
    .right {
      flex-grow: 1;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      font-size: 0.2rem;
      margin-right: 0.1rem;
      > div {
        margin-left: 0.35rem;
      }
      .friend {
        width: 0.2rem;
        height: 0.2rem;
      }
    }
  }
  .header-scroll {
    border-bottom: 0.01rem solid var(--border-gray);
    background-color: var(--white);
    background-image: linear-gradient(
      to bottom,
      rgba(255, 255, 255, 1),
      rgba(255, 255, 255, 1)
    );
    color: var(--text-black);
    svg,
    path {
      fill: var(--text-black);
    }
  }
  .swipe {
    width: 100%;
    .my-swipe .van-swipe-item {
      width: 100%;
      color: var(--text-white);
      font-size: 20px;
      line-height: 150px;
      text-align: center;
      background-color: #39a9ed;
    }
  }
  .store-content {
    width: 100%;
    box-sizing: border-box;
    padding: 0.15rem 0.2rem;
    background-color: var(--bgc-white);
    .store-title {
      font-size: 0.18rem;
      font-weight: bold;
      margin-bottom: 0.1rem;
    }
    .des {
      font-size: 0.15rem;
      color: var(--text-gray2);
      > div {
        margin: 0.02rem 0;
      }
    }
    .store-price {
      width: 100%;
      margin: 0.1rem 0;
      display: flex;
      align-items: center;
      > span {
        margin-right: 0.1rem;
      }
      .p {
        font-size: 0.2rem;
        color: var(--text-red);
      }
      .op {
        text-decoration: line-through;
        font-size: 0.12rem;
        color: var(--text-gray);
      }
      .tag {
        font-size: 0.13rem;
        padding: 0.02rem;
        color: var(--text-red);
        background-color: var(--bgc-red);
        border-radius: 0.02rem;
      }
    }
    .store-vip {
      position: relative;
      width: 3.3rem;
      height: 0.45rem;
      margin: 0.1rem auto;
      .vip-text {
        position: absolute;
        left: 0.46rem;
        bottom: 0;
        top: 0;
        margin: auto 0;
        font-size: 0.15rem;
        line-height: 0.48rem;
        color: var(--text-lv);
      }
    }
    .store-sell {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 0.12rem;
      color: var(--text-gray2);
    }
  }
  .promotion {
    width: 100%;
    display: flex;
    font-size: 0.17rem;
    padding: 0.15rem;
    box-sizing: border-box;
    margin: 0.1rem 0;
    background-color: var(--bgc-white);
    color: var(--text-gray);
    .left {
      margin-right: 0.2rem;
    }
    .right {
      .promotion-item {
        display: flex;
        align-items: center;
        margin-bottom: 0.05rem;
        .promotion-item-tag {
          font-size: 0.13rem;
          color: var(--text-red);
          background-color: var(--bgc-red);
          padding: 0.02rem;
          margin-right: 0.05rem;
          border-radius: 0.02rem;
        }
        .promotion-item-text {
          color: var(--text-black);
        }
      }
    }
  }
  .my-cell {
    margin: 0.05rem 0;
    .my-select {
      display: flex;
      > div {
        margin-right: 0.05rem;
      }
    }
  }
  .comments {
    width: 100%;
    box-sizing: border-box;
    padding: 0 0.15rem;
    margin: 0.1rem 0;
    background-color: var(--bgc-white);
    .comments-header {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 0.18rem;
      color: var(--text-gray);
      padding: 0.1rem 0;
      .left {
        .title {
          font-size: 0.2rem;
          font-weight: bold;
          margin-right: 0.05rem;
          color: var(--text-black);
        }
      }
    }
    .all-comment-list {
      position: fixed;
      left: 0;
      top: 0;
      width: 100vw;
      height: 100vh;
      z-index: 100;
    }
  }
  .shoper {
    width: 100%;
    box-sizing: border-box;
    padding: 0.3rem 0.15rem;
    background-color: var(--bgc-white);
    .shoper-title {
      font-size: 0.2rem;
      font-weight: bold;
      margin: 0.1rem 0;
    }
    .shoper-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        width: 0.46rem;
        height: 0.46rem;
        border-radius: 0.23rem;
        overflow: hidden;
      }
      .midle {
        flex-grow: 1;
        margin-left: 0.1rem;
        color: var(--text-gray);
        font-size: 0.14rem;
        .name {
          font-size: 0.16rem;
          color: var(--text-black);
        }
      }
      .right {
        width: 0.74rem;
        height: 0.32rem;
        background-color: var(--bgc-yellow);
        border-radius: 0.16rem;
        font-size: 0.14rem;
        line-height: 0.32rem;
        text-align: center;
      }
    }
    .shoper-ss {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 0.2rem;
      .ss-item {
        flex: 1;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        font-size: 0.14rem;
      }
    }
  }
  .footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 0.5rem;
    box-sizing: border-box;
    padding: 0 0.1rem;
    border-top: 0.01rem solid var(--border-gray);
    background-color: var(--bgc-white);
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-black);
    .chat-icon {
      font-size: 0.13rem;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .icon {
        font-size: 0.17rem;
      }
    }
    .btn {
      width: 1.2rem;
      height: 0.4rem;
      font-size: 0.16rem;
      padding: 0 0.1rem;
      line-height: 0.4rem;
      border-radius: 0.2rem;
      background-color: var(--bgc-yellow);
      box-sizing: border-box;
      text-align: center;
    }
  }
  .popup-content {
    position: relative;
    width: 100%;
    box-sizing: border-box;
    padding: 0.15rem;
    padding-top: 0.3rem;
    font-size: 0.14rem;
    color: var(--text-black);
    .goods-info {
      width: 100%;
      display: flex;
      .img-box {
        min-width: 0.92rem;
        width: 0.92rem;
        height: 0.92rem;
        border-radius: 0.1rem;
      }
      .right {
        margin-left: 0.1rem;
        flex-grow: 1;
        box-sizing: border-box;
        padding: 0.1rem;
        .title {
          font-size: 0.16rem;
        }
        .price {
          margin-top: 0.1rem;
          .p {
            font-size: 0.14rem;
            color: var(--text-red);
          }
          .op {
            font-size: 0.12rem;
            color: var(--text-gray);
            text-decoration: line-through;
            margin-left: 0.1rem;
          }
        }
      }
    }
    .specification {
      width: 100%;
      margin: 0.2rem 0;
      .specification-title {
        margin-bottom: 0.1rem;
      }
      .specification-items {
        width: 100%;
        box-sizing: border-box;
        display: flex;
        flex-wrap: wrap;
        > div {
          height: 0.36rem;
          background-color: var(--bgc-gray);
          line-height: 0.36rem;
          text-align: center;
          padding: 0 0.08rem;
          border-radius: 0.04rem;
          margin-right: 0.05rem;
          transition: 0.3s;
        }
        .active {
          background-color: var(--bgc-yellow);
        }
      }
    }
    .count {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .count-title {
      }
    }
    .btn {
      position: fixed;
      left: 0;
      right: 0;
      bottom: 0.2rem;
      margin: 0 auto;
      width: 3.2rem;
    }
  }
}
</style>
<style lang='scss'>
.store {
  .van-button {
    color: var(--text-black) !important;
  }
}
</style>